var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({
    data: {
        apis: app.data.ipconfig,
        indicatorDots: true,
        autoplay: true,
        interval: 2000,
        duration: 500,
        carNum: 1,
        shopPrice: '',
        circular: true,
        isShow: false,
        isGuigeShow: false,
        isBuyShow: false,
        changeColor: false,
        dataid1: 0,
        dataid2: 0,
        dataid3: 0,
        dataid4: 0,
        speclistcount: 0,
        goodsId: 0,
        numcount: 1,
        isRight: 0,
        speclist1: "",
        speclist2: "",
        speclist3: "",
        speclist4: "",
        loading: ''
    },
    onLoad: function (data) {
        var that = this;
        console.log(wx.getStorageSync('openid'));
        wx.request({
          url: "https://shops.dckeji.cn" + '/wxxiaochengxu/goodsApi/getGoodsDetail?goodsId=' + data.shopid,
          data: { appid: app.data.appid, openid: wx.getStorageSync('openid')},
            // data: { appid: app.data.appid, openid: 'oW8v90BWw9Vkf85Zbvnf3nWLbOXg' },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res);
                for (var i = 0; i < res.data.res.speclist.length; i++) {
                    that.setData({
                        speclist1: res.data.res.speclist[0],
                        speclist2: res.data.res.speclist[1],
                        speclist3: res.data.res.speclist[2],
                        speclist4: res.data.res.speclist[3],
                    });
                }
                console.log(res.data.res.goodsDetailUrl.goodsDetailUrl);
                that.setData({
                    swiperbanner: res.data.res.goodsbannerlist,
                    label: res.data.res.labelslist,
                    speclist1: that.data.speclist1,
                    speclist2: that.data.speclist2,
                    speclist3: that.data.speclist3,
                    speclist4: that.data.speclist4,
                    speclistcount: res.data.res.speclist.length,
                    pingfen: res.data.res.pingfens,
                    shopdetail: res.data.res.goods,
                    shopPrice: res.data.res.goods.minprice,
                    goodsId: res.data.res.goods.goodsId,
                    pinglun: res.data.res.pinglun,
                    goodsDetailUrl:res.data.res.goodsDetailUrl.goodsDetailUrl,
                    loading: 'hidden',
                    flag: res.data.res.flag//是否收藏 1是收藏  0是未收藏
                })
                var article=that.data.goodsDetailUrl;
                WxParse.wxParse('article', 'html', article, that,5);
            }
        })

    },
    addCart: function () {
        var that = this;
        that.setData({
            isShow: (!that.data.isShow)
        })
    },
    addBuy: function () {
        var that = this;
        that.setData({
            isBuyShow: (!that.data.isBuyShow)
        })
    },
    selGuige: function () {
        var that = this;
        that.setData({
            isGuigeShow: (!that.data.isGuigeShow)
        })
    },
    closeView: function () {
        var that = this;
        that.setData({
            isShow: false,
            isGuigeShow: false,
            isBuyShow: false
        })
    },
    nocloseView: function (e) {
        //e.stopPropagation();
    },
    changeGGColor: function (e) {
        var id = e.target.id;
        var that = this;
        this.setData({
            dataid1: id,
            dataname1: e.currentTarget.dataset.itemval,
        })
        that.sendPrice();
    },
    changeGGColor2: function (e) {
        var id = e.target.id;
        var that = this;
        this.setData({
            dataid2: id,
            dataname2: e.currentTarget.dataset.itemval,
        })
        that.sendPrice();
    },
    changeGGColor3: function (e) {
        var id = e.target.id;
        var that = this;
        this.setData({
            dataid3: id,
            dataname3: e.currentTarget.dataset.itemval
        })
        that.sendPrice();
    },
    changeGGColor4: function (e) {
        var id = e.target.id;
        var that = this;
        this.setData({
            dataid4: id,
            dataname4: e.currentTarget.dataset.itemval
        })
        that.sendPrice();
    },
    changeKWColor: function (res) {
        var tasteArray = [];
        for (var i = 0; i < this.data.tasteArray.length; i++) {
            if (res.target.id == this.data.tasteArray[i].id) {
                tasteArray[i] = {id: i + 1, changeColor: true, name: this.data.tasteArray[i].name}
            } else {
                tasteArray[i] = {id: i + 1, changeColor: false, name: this.data.tasteArray[i].name}
            }

        }
        this.setData({
            tasteArray: tasteArray
        })
    },
    carAdd: function (event) {
        var that = this;
        //var shopPrice = 168;
        var con = that.data.carNum + 1;
        var key = 'carNum';
        var obj = {};
        if (con > 25) {
            obj[key] = 1;
        } else {
            obj[key] = con;
        }
        that.setData(obj);
        that.setData({
            numcount: con
        })
    },
    carReduce: function (event) {
        var that = this;
        var con = that.data.carNum;
        var key = 'carNum';
        var obj = {};
        if (con <= 1) {
            obj[key] = 1;
            that.setData(obj);
            that.setData({
                numcount: con
            })
        }
        else {
            con--;
            obj[key] = con;
            that.setData(obj);
            that.setData({
                numcount: con
            })
        }
    },
    selguigeBtn: function () {
        var that = this;
        var numcount = that.data.numcount;
        that.pdguige();
    },
    sendPrice: function () {
        var that = this;
        wx.request({
            url: that.data.apis + '/wxxiaochengxu/goodsApi/getpricebyspec',
            data: {
                goodsid: that.data.goodsId,
                childspec1: that.data.dataid1,
                childspec2: that.data.dataid2,
                childspec3: that.data.dataid3,
                childspec4: that.data.dataid4,
                minprice: that.data.shopPrice,
                speclistcount: that.data.speclistcount
            },
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                console.log(res.data.res + "****")
                that.setData({
                    shopPrice: res.data.res
                })
            }
        })
    },
    pdguige: function () {
        var that = this;
        var sc = that.data.speclistcount;
        if (sc == 1) {
            if (that.data.dataid1 == 0) {
                wx.showModal({
                    title: '提示',
                    content: '请先选择规格类型！',
                    showCancel: false
                })
            } else {
                that.setData({
                    isGuigeShow: false,
                    isRight: 1
                })
            }
        } else if (sc == 2) {
            if (that.data.dataid1 == 0 || that.data.dataid2 == 0) {
                wx.showModal({
                    title: '提示',
                    content: '请先选择规格类型！',
                    showCancel: false
                })
            } else {
                that.setData({
                    isGuigeShow: false,
                    isRight: 1
                })
            }
        } else if (sc == 3) {
            if (that.data.dataid1 == 0 || that.data.dataid2 == 0 || that.data.dataid3 == 0) {
                wx.showModal({
                    title: '提示',
                    content: '请先选择规格类型！',
                    showCancel: false
                })
            } else {
                that.setData({
                    isGuigeShow: false,
                    isRight: 1
                })
            }
        } else if (sc == 4) {
            if (that.data.dataid1 == 0 || that.data.dataid2 == 0 || that.data.dataid3 == 0 || that.data.dataid4 == 0) {
                wx.showModal({
                    title: '提示',
                    content: '请先选择规格类型！',
                    showCancel: false
                })
            } else {
                that.setData({
                    isGuigeShow: false,
                    isRight: 1
                })
            }
        }
    },
    addCartShops: function () {
        var that = this;
        that.pdguige();
        if (that.data.isRight == 0) {
            wx.showModal({
                title: '提示',
                content: '请先选择规格类型！',
                showCancel: false
            })
        } else {
            wx.request({
                url: that.data.apis + '/wxxiaochengxu/registerApi/ifregister',
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    openid: wx.getStorageSync('openid'),
                    appid: app.data.appid
                },
                success: function (res) {
                    console.log(that.data.goodsId+'------')
                    if (res.data.status == "200") {
                        wx.request({
                            url: that.data.apis + '/wxxiaochengxu/goodsApi/validatestock',
                            method: 'GET',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            data: {
                                openid: wx.getStorageSync('openid'),
                                appid: app.data.appid,
                                goodsCount:that.data.numcount,
                                goodsid:that.data.goodsId,
                                childspec1:that.data.dataid1,
                                childspec2:that.data.dataid2,
                                childspec3:that.data.dataid3,
                                childspec4:that.data.dataid4,
                                size:that.data.speclistcount
                            },
                            success: function (res) {
                                if (res.data.res == 1) {
                                    wx.request({
                                        url: that.data.apis + '/wxxiaochengxu/goodsApi/addgoodscar',
                                        data: {
                                            appid: app.data.appid,
                                            openid: wx.getStorageSync('openid'),
                                            goodsid: that.data.goodsId,
                                            goodsCount: that.data.numcount,
                                            childspec1: that.data.dataid1,
                                            childspec2: that.data.dataid2,
                                            childspec3: that.data.dataid3,
                                            childspec4: that.data.dataid4,
                                            size: that.data.speclistcount
                                        },
                                        header: {
                                            'content-type': 'application/json'
                                        },
                                        success: function (res) {
                                            console.log(res.data.res + "****");
                                            that.setData({
                                                isShow: false
                                            })
                                            wx.showToast({
                                                title: '添加购物车成功',
                                                icon: 'success',
                                                duration: 1000
                                            })
                                            wx.switchTab({
                                                url: '../../pages/cart/index',
                                                success: function (e) {
                                                    var page = getCurrentPages().pop();
                                                    if (page == undefined || page == null) return;
                                                    page.onLoad();
                                                }
                                            })
                                        }
                                    })
                                } else {
                                    wx.showModal({
                                        title: '提示',
                                        content: '库存数量不足，请减少购买数量！',
                                        showCancel: false
                                    })
                                }
                            }
                        })
                    } else {
                        /*wx.showModal({
                         title: '提示',
                         content: '请前去注册！',
                         showCancel: false
                         })*/
                        wx.navigateTo({
                            url: '../../pages/register/index'
                        });
                    }
                }
            })
        }
    },
    addBuyShops: function () {
        var that = this;
        //console.log(that.data.goodsId);
        //console.log(that.data.numcount);
        //console.log(that.data.shopPrice);
        var SumMoney = (that.data.numcount) * (that.data.shopPrice);
        that.pdguige();
        if (that.data.isRight == 0) {
            wx.showModal({
                title: '提示',
                content: '请先选择规格类型！',
                showCancel: false
            })
        } else {
            //console.log('立即购买');

            wx.request({
                url: that.data.apis + '/wxxiaochengxu/registerApi/ifregister',
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    openid: wx.getStorageSync('openid'),
                    appid: app.data.appid
                },
                success: function (res) {
                    if (res.data.status == "200") {
                        wx.request({
                            url: that.data.apis + '/wxxiaochengxu/goodsApi/validatestock',
                            method: 'GET',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            data: {
                                openid: wx.getStorageSync('openid'),
                                appid: app.data.appid,
                                goodsCount:that.data.numcount,
                                goodsid:that.data.goodsId,
                                childspec1:that.data.dataid1,
                                childspec2:that.data.dataid2,
                                childspec3:that.data.dataid3,
                                childspec4:that.data.dataid4,
                                size:that.data.speclistcount
                            },
                            success: function (res) {
                                if (res.data.res == 1) {
                                    wx.navigateTo({
                                        url: '../../pages/orderBegin/index?id=' + that.data.goodsId + '&SumMoney=' + SumMoney + '&shopdj=' + that.data.shopPrice + '&shopCount=' + that.data.numcount
                                        + '&dataname1=' + that.data.dataname1 + '&dataname2=' + that.data.dataname2 + '&dataname3=' + that.data.dataname3 + '&dataname4=' + that.data.dataname4
                                        + '&childspec1=' + that.data.dataid1 + '&childspec2=' + that.data.dataid2 + '&childspec3=' + that.data.dataid3 + '&childspec4=' + that.data.dataid4
                                        + '&shopname=' + that.data.shopdetail.goodsName + '&shoppic=' + that.data.shopdetail.goodsListPicUrl
                                    })
                                } else {
                                    wx.showModal({
                                        title: '提示',
                                        content: '库存数量不足，请减少购买数量！',
                                        showCancel: false
                                    })
                                }
                            }
                        })
                    } else {
                        /*wx.showModal({
                         title: '提示',
                         content: '请前去注册！',
                         showCancel: false
                         })*/
                        wx.navigateTo({
                            url: '../../pages/register/index'
                        });
                    }
                }
            })
        }
    },
    //点击了收藏按钮
    clickCollect:function(e){
        //先判断当前的状态
        //判断当前的flag
        var that = this;
        var currentFlag = that.data.flag 
        if (currentFlag == 1){
          currentFlag = 0;
        }else{
          currentFlag = 1;
        }
        wx.request({
          url: that.data.apis +'/wxxiaochengxu/goodsApi/Collection',
          
          data: { appid: app.data.appid, openid: wx.getStorageSync('openid'), goodsid: that.data.goodsId, flag: currentFlag },
          //测试用
          // data: { appid: app.data.appid, openid: 'oW8v90BWw9Vkf85Zbvnf3nWLbOXg', goodsid: that.data.goodsId, flag: currentFlag},
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res);
            // that.data.highNames[i][highInfos[that.data.highIds[i]][0].lottery_id];
            that.setData({
              flag: currentFlag
            })
          }
        })
        
    }
});

